package oop.core.control;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import oop.core.med.AvatarMovementMediator;
import oop.core.med.PauseMediator;

/**
 * Reads in input from the keyboard and mouse and redirects 
 * meaningful information to the appropriate mediators.
 * @author Alex Guthrie
 */


public class Controller implements KeyListener {
	
	// TODO: Move keyMap into another wrapper class/interface implementation.
	private Map<Character, Integer> config;
	private AvatarMovementMediator avatarMovementMediator;

	public static List<Integer> ALL_MOVE_DIRECTIONS;
	public static int NORTH = 8;
	public static int NORTH_WEST = 7;
	public static int SOUTH_WEST = 1;
	public static int WEST = 4;
	public static int EAST = 6;
	public static int NORTH_EAST = 9;
	public static int SOUTH_EAST = 3;
	public static int SOUTH = 2;
	public static int PAUSE = 0;	// Not really a direction.
	public static int HIDE = 10;  	//For sneak to hide
	
	public static char INVALID_CHAR = 'Q';
	private PauseMediator pauseMediator;
	
	public Controller() {
		config = new HashMap<Character, Integer>();
		
		ALL_MOVE_DIRECTIONS = new ArrayList<Integer>();
		ALL_MOVE_DIRECTIONS.add(NORTH);
		ALL_MOVE_DIRECTIONS.add(EAST);
		ALL_MOVE_DIRECTIONS.add(WEST);
		ALL_MOVE_DIRECTIONS.add(SOUTH);
		ALL_MOVE_DIRECTIONS.add(NORTH_EAST);
		ALL_MOVE_DIRECTIONS.add(NORTH_WEST);
		ALL_MOVE_DIRECTIONS.add(SOUTH_EAST);
		ALL_MOVE_DIRECTIONS.add(SOUTH_WEST);
		ALL_MOVE_DIRECTIONS.add(PAUSE);
//		ALL_MOVE_DIRECTIONS.add(HIDE);
		resetDefaults();
	}
	
	public Map<Character, Integer> getConfig()
	{
		return config;
	}
	
	public void setPauseMediator(PauseMediator pauseMediator)
	{
		this.pauseMediator = pauseMediator;
	}
	
	public void setMovementMediator(AvatarMovementMediator avatarMovementMediator)
	{
		this.avatarMovementMediator = avatarMovementMediator;
	}

	/**
	 * Handles the event that a key is pressed. Looks the key up in
	 * the keyMap HashMap, and then determines if the key is 
	 * meaningful or not. Redirects meaningful information.
	 */
	public void keyPressed(KeyEvent e) {
		char c = e.getKeyChar();
		
		if(!config.containsKey(c))
			return;
		
		int i = config.get(c);
		if( i > 0 && i <= 9 ) // movement mediator handles...
		{
			avatarMovementMediator.moveEntity( i );
		}
		else if ( i == 0) {
			pauseMediator.toggle();
		}
		
		else if ( i == 10)
		{
			avatarMovementMediator.toggleHide();
		}
		else {
			//not a valid key press
			//do nothing
		}
	}

	public void keyReleased(KeyEvent arg0) {
		//Does nothing
		
	}

	public void keyTyped(KeyEvent arg0) {
		//Does nothing
		
	}

	/**
	 * This function allows the user to set a new keyMap
	 * based on what they changed in the ControllerConfigMenu
	 * @param keyMap
	 */
	public void setKeyMap( Map<Character,Integer> keyMap ) {
		this.config = keyMap;
	}
	
	/**
	 * This method resets the original default keyMap
	 */
	public void resetDefaults() {
		config.clear();
		config.put('1', 1);
		config.put('2', 2);
		config.put('3', 3);
		config.put('4', 4);
		config.put('6', 6);
		config.put('7', 7);
		config.put('8', 8);
		config.put('9', 9);
		config.put('1', 1);
		config.put('p', 0);
		config.put('`', 10);
		
	}


}